package com.pengpai.pangucms.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pengpai.pangucms.entity.SysFileinfo;
import com.pengpai.pangucms.service.ISysFileinfoService;
import com.pengpai.pangucms.utils.DateUtils;
import com.pengpai.pangucms.utils.PageDomain;
import com.pengpai.pangucms.utils.R;
import com.pengpai.pangucms.utils.ResultTable;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Controller
@RequestMapping("/webAttach")
public class WebAttachController {

    @Resource
    private ISysFileinfoService sysFileinfoService;

    @GetMapping("index")
    @PreAuthorize("hasAuthority('web:attach:read')")
    public String index(){
        return "admin/web_attach/web_attach_index";
    }

    @GetMapping("/list")
    @ResponseBody
    @PreAuthorize("hasAuthority('web:attach:read')")
    public ResultTable data(SysFileinfo sysFileinfo, PageDomain pageDomain) {
        Page<SysFileinfo> sysFileinfoPage = new Page<>(pageDomain.getPage(), pageDomain.getLimit());
        QueryWrapper<SysFileinfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_attach", "1")
                .like(StrUtil.isNotBlank(sysFileinfo.getFileName()), "file_name", sysFileinfo.getFileName())
                .or().like(StrUtil.isNotBlank(sysFileinfo.getFileSuff()), "file_suff",sysFileinfo.getFileSuff())
                .orderByDesc("upload_time");
        List<SysFileinfo> sysUserList = sysFileinfoService.page(sysFileinfoPage,queryWrapper).getRecords();
        return ResultTable.pageTable(sysFileinfoPage.getTotal(), sysUserList);
    }

    @GetMapping("add")
    @PreAuthorize("hasAuthority('web:attach:create')")
    public String add(){
        return "admin/web_attach/web_attach_add";
    }

    @PostMapping(value = "/upload")
    @ResponseBody
    public Object upload(@RequestParam(value="file",required=false) MultipartFile file, HttpServletRequest request) throws Exception {
        if(file.isEmpty()) return R.fail("文件上传失败！");
        String path = sysFileinfoService.upload(file, true);
        return R.success(path);
    }

    @PostMapping("/del")
    @ResponseBody
    @PreAuthorize("hasAuthority('web:attach:delete')")
    public R del(String ids){
        List<Long> list = null;
        if(StrUtil.isNotBlank(ids)){
            list = Stream.of(ids.split(",")).map(Long::parseLong).collect(Collectors.toList());
        }
        sysFileinfoService.removeBatchByIds(list);
        return R.success();
    }

    @GetMapping("/edit")
    @PreAuthorize("hasAuthority('web:attach:update')")
    public String edit(String id, Model model){
        SysFileinfo attach = sysFileinfoService.getById(id);
        String dateString = DateUtils.toDateString(attach.getUploadTime(), "yyyy-MM-dd HH:mm:ss");
        model.addAttribute("dateString", dateString);
        model.addAttribute("attach", attach);
        return "admin/web_attach/web_attach_edit";
    }
}
